Skip to content

Conversation

@Quuxplusone
Copy link
Contributor

@W-E-Brown reports that in the example in [dcl.type.auto.deduct], my P2266 "Simpler implicit move" introduced an example which, while correct in the point it's illustrating, is ill-formed for another reason.

auto f1(int x) -> decltype((x)) { return (x); }  // return type is int&
auto f2(int x) -> decltype(auto) { return (x); }  // return type is int&&

GCC rightly complains about f1:

error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
 8 | auto f1( int x ) -> decltype( (x) ) { return (x); }

That is, the Standard's example is correct that f1's deduced return type is int&, but then the function body is ill-formed because you can't bind that int& to the rvalue expression (x).

f1's function body exists at all here only to create a sort of tutorial parallelism to f2. We can discard that parallelism; then f1 becomes well-formed, while still illustrating the same point.


Question for discussion: Walter also points out that even though f2 is well-formed, it invariably returns a dangling reference to function parameter x. (GCC correctly detects this, and warns.) Is this a bad thing? If it is a bad thing, then can anyone think of a way to adjust the example so that it still illustrates something useful here, but doesn't have a dangling reference?

@eisenwave
Copy link
Member

To be clear, https://eel.is/c++draft/expr.prim.id.unqual#15.2 is the wording responsible for making the example ill-formed: a possibly parenthesized unqualified-id in a return statement is an xvalue.

I'm marking this P2 because there is clearly a bug, but it doesn't have normative impact (it's just an example).

@eisenwave eisenwave added the P2-Bug Presentational errors and omissions label Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2-Bug Presentational errors and omissions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants